package com.starlink.databaseservice.controller;

import com.starlink.common.BaseResult;
import com.starlink.databaseservice.dto.PostDto;
import com.starlink.databaseservice.mapper.PostMapper;
import com.starlink.databaseservice.model.Post;
import com.starlink.databaseservice.service.PostService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping(value = "post")
@RestController
public class PostController {

    private static final Logger log = LoggerFactory.getLogger(PostController.class);
    @Autowired
    private PostService postService;

    private PostMapper postMapper = PostMapper.INSTANCE;
    /*@Autowired
    private PostsService postsService;

    @GetMapping(value = "list")
    public void list() {
        postsService.load();
    }*/

    @GetMapping(value = "query")
    public BaseResult<PostDto> query(int page, int size) {
        Page<Post> pageInfo = postService.getList(page, size);

        return BaseResult.pageResult(pageInfo.getContent().stream().map(postMapper::postToPostDto).toList(),
                pageInfo.getTotalElements());
    }

    @PostMapping(value = "confirm")
    public BaseResult<String> confirm(Long id, Short type) {
        log.info("id is: {}, type is: {}", id, type);
        postService.updateLabel(id, type);
        return  BaseResult.succes();
    }
}
